// pages/index/vehicleDetails.js
var $api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sharePhone: null,
    errorShow:false,
    vehicleId: "", //车辆id
    carDetails: null,
    typeCloums: [{
      text: "车源已售",
      value: 1
    }, {
      text: "成交下架",
      value: 2
    },{
      text: "非他一手",
      value: 3
    },{
      text: "车型有误",
      value: 4
    },{
      text: "车况不符",
      value: 5
    },{
      text: "价格不实",
      value: 6
    },{
      text: "其他",
      value: 999
    },],

    //车辆详细信息
  },

  //分享
  shareTofriend: function (event) {
    console.log("分享到朋友圈");
    wx.navigateTo({
      url: '/pages/index/share?vehicleId=' + this.data.vehicleId,
    })
  },
  showErrorCorrection: function (e) {
    this.setData({
      errorShow: true
    })
  },
  closeError: function (e) {
    this.setData({
      errorShow: false
    })
  },
  errorCorrection: function (e) {
    $api.postError({car_id: this.data.vehicleId, type: e.detail.value.value}).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '提交成功!',
          icon: 'success',
          duration: 2000 //持续的时间
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000 //持续的时间
        })
      }
      
      this.setData({
        errorShow: false
      })
    });
  },

  helpSell: function (e) {
    $api.addHelpSell({car_id: this.data.vehicleId}).then(res => {
      if (res.code == 1) {
        this.setData({
          'carDetails.helped': res.data
        });
        wx.showToast({
          title: res.data ? '已加入帮卖' : '已取消帮卖',
          icon: 'success',
          duration: 2000 //持续的时间
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000 //持续的时间
        })
      }
    });
  },

  previewImg: function (e) {
    console.log(e);
    wx.previewImage({
      urls: this.data.carDetails.images.split(','),
      current: e.currentTarget.dataset.img,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  //电话联系
  contact: function (e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.scene) {
      const scene = decodeURIComponent(options.scene);
      var arr = scene.split(',');
      this.setData({
        vehicleId: arr[0],
        sharePhone: arr[1]
      })
    } else {
      if (options.phone) {
        this.setData({
          sharePhone: options.phone
        })
      }
      this.setData({
        vehicleId: options.vehicleId
      })
    }


  },
  onShow: function () {
    this.getGoodsDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getGoodsDetail: function () {
    let params = {
      goods_id: this.data.vehicleId
    };
    $api.getGoodsDetail(params).then(res => {
      if (res.code == 1) {
        this.setData({
          carDetails: res.data
        })
        console.log(this.data.carDetails);
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userInfo = wx.getStorageSync('userInfo');
    let phone = userInfo.mobile ? userInfo.mobile : '';
    return {
      title: this.data.carDetails.brand_name + ' ' + this.data.carDetails.name + ' ' + this.data.carDetails.car_displacement + 'L',
      path: '/pages/index/vehicleDetails?vehicleId='+ this.data.vehicleId + 'phone=' + phone,
      imageUrl: this.data.carDetails.image
    }
  }
})